<template>
  <el-dialog :visible.sync="visible" width="80%" custom-class="default-dialog" :close-on-click-modal="false"
    @close="cancel" append-to-body v-dialogDrags>
    <div slot="title">
      <img v-if="type" :src="require(`@/assets/img/${type}.png`)" alt="">
      <span>{{ title }} </span>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="120px" class="dialog-form" :disabled="type == 'view'">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="指标名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入指标名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="指标类型" >
            <el-select size="small" v-model="form.type"  placeholder="请选择">
              <el-option
                v-for="item in zblxOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="准则层" prop="criterion">
            <el-input v-model="form.criterion" placeholder="请输入准则层"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="指标层" prop="metrics">
            <el-input v-model="form.metrics" placeholder="请输入指标层"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="总得分" prop="totalScore">
            <el-input type="number" v-model="form.totalScore" placeholder="请输入总得分"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="来源" prop="source">
            <el-input v-model="form.source" placeholder="请输入来源"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="性质" prop="attribute">
            <el-input v-model="form.attribute" placeholder="请输入性质"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" v-if="type != 'view'" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      title: '',
      zblxOptions:[
        {
          value: "river",
          label: '河流指标'
        },
        {
          value: "lake",
          label: '湖泊指标'
        },
        {
          value: "manage",
          label: '管理指标'
        }
      ],
      form: {
        dictName: '',
        hehuType: []
      },
      hehuOptions: [
        {
          value: 'option1',
          label: '流域1',
          children: [
            {
              value: 'option1-1',
              label: '子流域1-1',
              children: [
                {
                  value: 'option1-1-1',
                  label: '单元1-1-1'
                }
              ]
            }
          ]
        },
        {
          value: 'option2',
          label: '流域2',
          children: [
            {
              value: 'option2-1',
              label: '子流域2-1',
              children: [
                {
                  value: 'option2-1-1',
                  label: '单元2-1-1'
                }
              ]
            }
          ]
        }
      ],
      rules: {
        attribute: [{ required: true, message: '请输入性质', trigger: 'blur' }],
        criterion: [{ required: true, message: '请输入准测层', trigger: 'blur' }],
        metrics: [{ required: true, message: '请输入指标层', trigger: 'blur' }],
        name: [{ required: true, message: '请输入指标名称', trigger: 'blur' }],
        source:[{ required: true, message: '请输入来源', trigger: 'blur' }],
        totalScore:[{ required: true, message: '请输入总得分', trigger: 'blur' }],
      },
      type: ''
    };
  },
  methods: {
    showModal(type, row) {
      this.visible = true
      this.type = type
      if (type == 'add') {
        this.title = '新增'
        this.form = {
          "dictName": "",
          "remark": "",
        }
      } else if (type == 'edit') {
        this.title = '编辑'
        this.getInfoInit(row.id)
      } else if (type == 'view') {
        this.title = '详情'
        this.getInfoInit(row.id)
      }
    },
    // 详情
    getInfoInit(id) {
      console.log(`详情id：${id}`)
      this.$api.getHeHuMetrics({ id: id }).then(res => {
        if (res && res.code == 200) {
          let data = res.data || {}
          this.form = data
        }
      })
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.type == 'add') {
            this.$api.addHeHuMetrics(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          } else if (this.type == 'edit') {
            this.$api.updateHeHuMetrics(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          }
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.$refs["form"].resetFields();
    },
  }
};
</script>
<style lang="less" scoped>
.el-form-item__label {
  width: 150px;
}
</style>
